package com.jy.rhin.valid;


import com.jy.api.misc.MiscAPI;
import com.jy.api.misc.dto.DaiMaDto;
import com.jy.api.misc.dto.ZhiYuMXCXDto;
import com.jy.framework.base.entity.Result;
import com.jy.rhin.infra.cda.EBianMa2ZhiYu;
import com.jy.rhin.infra.cda.feign.ShuYuService;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;
import org.seimicrawler.xpath.JXNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * @author WeiQ
 * @date 2023/3/27 10:48
 */
@Component
public class MiscValid {

    @Resource
    ShuYuService shuYuService;
    
    @Autowired
    MiscAPI miscAPI;

    
    public Result<Boolean> valid(MiscCodeObject code) {
    	return miscAPI.bianMaXTZhiExist(code.getCodeSystem(), code.getCode(), code.getDisplayName());
    }
    public Map<String, String> valid(List<MiscCodeObject> miscCodeObjectList) {
        //入参的待验证值域
        ZhiYuMXCXDto[] zhiYuMXCXDtos = miscCodeObjectList
                .stream()
                .collect(Collectors.groupingBy(MiscCodeObject::getCodeSystem))
                .entrySet()
                .stream()
                .map(entiry -> {
                    ZhiYuMXCXDto dto = new ZhiYuMXCXDto();
                    dto.setZhiJiBSF(EBianMa2ZhiYu.lookup(entiry.getKey()));
                    List<MiscCodeObject> value = entiry.getValue();
                    dto.setCodeList(value.stream().map(MiscCodeObject::getCode).collect(Collectors.toList()));
                    return dto;
                })
                .toArray(ZhiYuMXCXDto[]::new);
        //从术语系统获取待验证值域的编码系统信息
        Map<String, List<DaiMaDto>> miscListBySjj = shuYuService.getValueByCode(zhiYuMXCXDtos);
        Map<String, String> msg = new HashMap<>();
        //开始核对 遍历每一条待验证参数
        for (MiscCodeObject miscCodeObject : miscCodeObjectList) {
            //根据待验证的值集标识符，取到编码系统的值域 只需要获取code和displayName
            Map<String, String> wssybmxtDaiMaDtos = Collections.emptyMap();
            String bsf = EBianMa2ZhiYu.lookup(miscCodeObject.getCodeSystem());
            if (!CollectionUtils.isEmpty(miscListBySjj.get(bsf))) {
                wssybmxtDaiMaDtos = miscListBySjj.get(bsf)
                        .stream()
                        .collect(Collectors.toMap(DaiMaDto::getCode, DaiMaDto::getValue));
            }
            //判断待验证的code，是否在编码系统
            if (!wssybmxtDaiMaDtos.containsKey(miscCodeObject.getCode())) {
                msg.put(miscCodeObject.getJxNode().asString(), String.format("编码系统[%s]中，不包含code[%s]，请核对编码系统", miscCodeObject.getCodeSystem(), miscCodeObject.getCode()));
                continue;
            }
            //判断待验证的displayName，是否与编码系统相同
            if (!StringUtils.equals(wssybmxtDaiMaDtos.get(miscCodeObject.getCode()), miscCodeObject.getDisplayName())) {
                msg.put(miscCodeObject.getJxNode().asString(), String.format("参数中code[%s]配置的displayName[%s]，与编码系统[%s]中的名称[%s]不符", miscCodeObject.getCode(), miscCodeObject.getDisplayName(), miscCodeObject.getCodeSystem(), wssybmxtDaiMaDtos.get(miscCodeObject.getCodeSystem())));
                continue;
            }
        }
        return msg;
    }

    @Data
    public static class MiscCodeObject {

        public MiscCodeObject(JXNode jxNode, String basePath) {
            this.jxNode = jxNode;
            this.codeSystem = jxNode.selOne("@codeSystem").asString();
            this.code = jxNode.selOne("@code").asString();
            this.displayName = jxNode.selOne("@displayName").asString();
            this.basePath = basePath + jxNode.asString();
        }

        JXNode jxNode;
        String codeSystem;
        String code;
        String displayName;
        String basePath;

    }

}
